package com.xbongbong.workflow.service.field.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.LinkInfoPojo;
import com.xbongbong.paas.help.ConditionListHelp;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.workflow.pojo.FilterCondition;
import com.xbongbong.workflow.pojo.WorkflowRulePojo;
import com.xbongbong.workflow.service.field.AbstractWorkflowConditionFieldService;
import com.xbongbong.workflow.service.field.WorkflowConditionFieldService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/06/07 14:03
 */
@Service("workflowConditionLinkDataServiceImpl")
public class WorkflowConditionLinkDataServiceImpl extends AbstractWorkflowConditionFieldService implements WorkflowConditionFieldService {


    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.LINK_DATA.getType());
    }




    @Override
    public Object handlerThisFormField(PaasFormDataEntityExt paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField,
                                       WorkflowNodeEntity workflowNodeEntity, FilterCondition filterCondition, WorkflowRulePojo workflowRulePojo) throws XbbException {
        Object object = getLinkData(data.get(fieldAttrEntity.getAttr()), fieldAttrEntity, targetField, workflowRulePojo);
        if (Objects.equals(FieldTypeEnum.LINK_DATA.getType(), fieldAttrEntity.getFieldType()) && Objects.equals(fieldAttrEntity.getFieldType(), targetField.getFieldType())) {
            object = data.get(fieldAttrEntity.getAttr());
            if (Objects.equals(0, object)) {
                return null;
            }
        }
        return object;
    }


    @Override
    public Object handlerTargetFormField(Object object, PaasFormDataEntityExt paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, WorkflowNodeEntity workflowNodeEntity, WorkflowRulePojo workflowRulePojo, FilterCondition filterCondition) throws XbbException {
        return getLinkData(object, fieldAttrEntity, targetField, workflowRulePojo);
    }

    private Object getLinkData(Object value, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, WorkflowRulePojo workflowRulePojo) throws XbbException {
        Object object = value;
        if (ConditionListHelp.validateIsLinkData(fieldAttrEntity.getFieldType(), targetField.getFieldType())
                && Objects.nonNull(object) && !Objects.equals(0, object) && object instanceof Number) {
            LinkInfoPojo linkInfoPojo = fieldAttrEntity.getLinkInfo();
            String key = object.toString();
            if (Objects.equals(SaasMarkEnum.SAAS.getCode(), linkInfoPojo.getLinkSaasMark())) {
                key = linkInfoPojo.getLinkBusinessType() + "_" + key;
            }
            if (Objects.isNull(workflowRulePojo.getFormDataMap())) {
                return null;
            }
            PaasFormDataEntityExt formData = workflowRulePojo.getFormDataMap().get(key);
            if (Objects.isNull(formData)) {
//                LOGGER.error("error is empty：" + key);
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_DATA_NOT_EXIST);
            }
            String linkKey = linkInfoPojo.getLinkKey();
            if (Objects.equals(FieldTypeEnum.SERIALNO.getAlias(), linkKey)) {
                return formData.getSerialNo();
            }
            JSONObject targetData = formData.getData();
            if (Objects.nonNull(targetData)) {
                object = targetData.get(linkKey);
                return object;
            }
        } else if (Objects.isNull(object) || Objects.equals(0, object) || StringUtil.isEmpty(object.toString())) {
            return null;
        }
        return object;
    }
}
